package com.hrms.domain;

import java.text.ParseException;
import java.util.Map;
import java.util.logging.Level;

import javax.jdo.annotations.PersistenceCapable;
import javax.servlet.http.HttpServletRequest;

import com.hrms.cache.MyDashBoardCache;
import com.hrms.core.Constants;

@PersistenceCapable(detachable="true")
public class MyDashboard extends DashBoard {
	
	public static final String TYPE = MyDashboard.class.getSimpleName();
	
	@Override
	public String fireEvents(String userAction, HttpServletRequest request)
			throws Exception {
		log.log(Level.INFO, getUserId());
		MyDashboard mydb = BusinessObjectImpl.selectAsSingleResult(MyDashboard.class, "where userId==:p0" , new Object[]{ getUserId().toLowerCase() } );
		if(mydb != null){
			setKey(mydb.getKey());
			
			MyDashboardLog dbLog = new MyDashboardLog();  
			dbLog.setLastModifiedTimestamp(mydb.getCreateTimestamp());
			dbLog.setUserId(mydb.getUserId());
			dbLog.setUtilization(mydb.getUtilization());
			dbLog.save();
			
		}
		
		String executionResult = super.fireEvents(userAction, request);
		if(Constants.UNKNOWN_REQUEST.equals(executionResult)){
			return "Failed to update Utilization. Try again later.";
		}else{
			return "Utilization Updated Successfuly.";
		}
		
	}

	@Override
	protected void performPostPersistEvents(String userAction,
			HttpServletRequest request) throws Exception {
		
		// updating memcache on the object
		MyDashBoardCache.get().putInCache(getUserId(), this);	
		super.performPostPersistEvents(userAction, request);
	}
	
	public static MyDashboard getByUserId(String userId){
		userId = userId.toLowerCase();
		
		MyDashboard emp = null;
		
		emp = MyDashBoardCache.get().findInCache(userId);		
		if(emp!=null){
			return emp;
		}
		
		emp = selectAsSingleResult(MyDashboard.class, "where userId==:p0" , new Object[]{ userId });
		if(emp!=null){
			MyDashBoardCache.get().putInCache(userId, emp);			
		}
		
		return emp;
	}

	public static String getFormattedUtilizationForUser(String userId){		
		MyDashboard mydb = getByUserId(userId);
		
		if((mydb != null) && (mydb.getUtilization() != null)){
			
			int ratin = mydb.getUtilization().getRating();

			if((ratin == 1) || ratin ==2){ //green
				return "style=\"border-color:#01DF01; border-style: solid;\"";
				
			}else if((ratin == 3) || ratin == 4){ //light purple | prev orange  #FF8000
				return "style=\"border-color:#800080; border-style: solid;\"";
				
			}else if(ratin == 5){ //red
				return "style=\"border-color:#FF0000; border-style: solid;\"";
			}
			 
		}
		return Constants.EMPTY_STRING;
	}

	public MyDashboard() {
		super();
	}

	public MyDashboard(Map<String, String[]> parameterMap, String prefix)
			throws IllegalArgumentException, IllegalAccessException,
			ParseException {
		super(parameterMap, prefix);
	}

	public MyDashboard(Map<String, String[]> parameterMap)
			throws IllegalArgumentException, IllegalAccessException,
			ParseException {
		super(parameterMap);
	}

	

	public String validate() {
		return null;
	}

	public void setLastModifyUser(String lastModifyUser) {

	}

	public void setCreatedByUser(String createdByUser) {

	}

	public String getNamer() {
		return null;
	}

}
